package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.DistributorMessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.rabbitmq.binders.CrmConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.CrmBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.CrmFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;

import javax.annotation.Resource;

/**
 * crm 消息消费者
 * @author 魏荣杰
 * @date 2019/2/25 14:04
 * @since v1.0
 * @version v1.0
 */
@EnableBinding(CrmConsumerBinder.class)
public class CrmListener {
    private static Logger logger = LoggerFactory.getLogger(CrmListener.class);

    @Resource
    private CrmFeignClient crmFeignClient;

    @StreamListener(value = CrmBinderConstant.CUSTOMER_INPUT)
    public void customerReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case CUSTOMER_ADD_PUSH:
                    response = crmFeignClient.customerAdd(message);
                    break;
                case CUSTOMER_CHANGE_PUSH:
                    response = crmFeignClient.customerChange(message);
                    break;
                case CUSTOMER_TEAM_ADD_DEL_MAIN:
                    response = crmFeignClient.customerTeamMain(message);
                    break;
                case CUSTOMER_TEAM_ADD_DEL_SYNERGETIC:
                    response = crmFeignClient.customerTeamSynergetic(message);
                    break;
                case CUSTOMER_STAGE:
                    response = crmFeignClient.customerStage(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.customerReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.customerReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.CUSTOMER_HANDOVER_INPUT)
    public void customerHandoverReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case CUSTOMER_HANDOVER_PUSH:
                    response = crmFeignClient.customerHandover(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.customerHandoverReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.customerHandoverReceive Exception, type=" + type, e);
        }

    }

    @StreamListener(value = CrmBinderConstant.CUSTOMER_DISTRIBUTION_INPUT)
    public void customerDistributionReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case CUSTOMER_DISTRIBUTION_PUSH:
                    response = crmFeignClient.customerDistribution(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.customerDistributionReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.customerDistributionReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.CONTACT_INPUT)
    public void contactReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case CONTACT_NEW:
                    response = crmFeignClient.contactAdd(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.contactReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.contactReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.OPPORTUNITY_INPUT)
    public void opportunityReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case OPPORTUNITY_ADD_PUSH:
                    response = crmFeignClient.opportunityAdd(message);
                    break;
                case OPPORTUNITY_CHANGE_PUSH:
                    response = crmFeignClient.opportunityChange(message);
                    break;
                case OPPORTUNITY_TEAM_ADD_DEL_MAIN_PUSH:
                    response = crmFeignClient.opportunityTeamMain(message);
                    break;
                case OPPORTUNITY_TEAM_ADD_DEL_SYNERGETIC_PUSH:
                    response = crmFeignClient.opportunityTeamSynergetic(message);
                    break;
                case OPPORTUNITY_STAGE_PUSH:
                    response = crmFeignClient.opportunityStage(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.opportunityReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.opportunityReceive Exception, type=" + type, e);
        }

    }

    @StreamListener(value = CrmBinderConstant.COMMUNICATE_PLAN_INPUT)
    public void communicatePlanReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case COMMUNICATE_PLAN_ADD_PUSH:
                    response = crmFeignClient.communicatePlanAdd(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.communicatePlanReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.communicatePlanReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.COMMUNICATE_INPUT)
    public void communicateReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case COMMUNICATE_ADD_PUSH:
                    response = crmFeignClient.communicateAdd(message);
                    break;
                case COMMUNICATE_LIKE_PUSH:
                    response = crmFeignClient.communicateLike(message);
                    break;
                case COMMUNICATE_COMMENT_PUSH:
                    response = crmFeignClient.communicateComment(message);
                    break;
                case COMMUNICATE_REPLY_PUSH:
                    response = crmFeignClient.communicateReplay(message);
                    break;
                case COMMUNICATE_AT_PUSH:
                    response = crmFeignClient.communicateAt(message);
                    break;
                case COMMUNICATE_COMMENT_DEL_PUSH:
                    response = crmFeignClient.communicateCommentDel(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.communicateReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.communicateReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.CONTRACT_INPUT)
    public void contractReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case CONTRACT_ADD_PUSH:
                    response = crmFeignClient.contractAdd(message);
                    break;
                case CONTRACT_STAGE_PUSH:
                    response = crmFeignClient.contractStagePush(message);
                    break;
                case CONTRACT_CHANGE_PUSH:
                    response = crmFeignClient.contractChange(message);
                    break;
                case CONTRACT_TEAM_ADD_DEL_MAIN_PUSH:
                    response = crmFeignClient.contractTeamMain(message);
                    break;
                case CONTRACT_TEAM_ADD_DEL_SYNERGETIC_PUSH:
                    response = crmFeignClient.contractTeamSynergetic(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.contractReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.contractReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.PAYMENT_INPUT)
    public void paymentReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case PAYMENT_PLAN_ADD:
                    response = crmFeignClient.paymentPlanAdd(message);
                    break;
                case PAYMENT_SHEET_ADD:
                    response = crmFeignClient.paymentSheetAdd(message);
                    break;
                case PAYMENT_SHEET_RED_ADD:
                    response = crmFeignClient.paymentSheetRedAdd(message);
                    break;
                case PAYMENT_SHEET_BAD_ADD:
                    response = crmFeignClient.paymentSheetBadAdd(message);
                    break;
                case PAYMENT_SHEET_PREPAYMENT_ADD:
                    response = crmFeignClient.paymentSheetPrepaymentAdd(message);
                    break;
                case PAYMENT_SHEET_RED_PREPAYMENT_ADD:
                    response = crmFeignClient.paymentSheetRedPrepaymentAdd(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.paymentReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.paymentReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.INVOICE_INPUT)
    public void invoiceReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case INVOICE_ADD_PUSH:
                    response = crmFeignClient.invoiceAdd(message);
                    break;
                case INVOICE_RED_ADD_PUSH:
                    response = crmFeignClient.invoiceRedAdd(message);
                    break;
                case INVOICE_DEL_PUSH:
                    response = crmFeignClient.invoiceDel(message);
                    break;
                case INVOICE_REDUCTION:
                    response = crmFeignClient.invoiceReduction(message);
                    break;
                case INVOICE_INVALIDATION:
                    response = crmFeignClient.invoiceInvalidation(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.invoiceReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.invoiceReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.PURCHASE_INVOICE_INPUT)
    public void purchaseInvoiceReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case PURCHASE_INVOICE_ADD_PUSH:
                    response = crmFeignClient.purchaseInvoiceAdd(message);
                    break;
                case PURCHASE_INVOICE_RED_ADD_PUSH:
                    response = crmFeignClient.purchaseInvoiceRedAdd(message);
                    break;
                case PURCHASE_INVOICE_DEL_PUSH:
                    response = crmFeignClient.purchaseInvoiceDel(message);
                    break;
                case PURCHASE_INVOICE_REDUCTION:
                    response = crmFeignClient.purchaseInvoiceReduction(message);
                    break;
                case PURCHASE_INVOICE_INVALIDATION:
                    response = crmFeignClient.purchaseInvoiceInvalidation(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.invoiceReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.invoiceReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.STATEMENT_INPUT)
    public void statementReceive(DistributorMessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case STATEMENT_GENERATE_PUSH:
                    response = crmFeignClient.statementGenerate(message);
                    break;
                case STATEMENT_REMIND_CONFIRM_PUSH:
                    response = crmFeignClient.statementRemindConfirm(message);
                    break;
                case STATEMENT_VALET_CONFIRM_PUSH:
                    response = crmFeignClient.statementValetConfirm(message);
                    break;
                case STATEMENT_REGENERATE_PUSH:
                    response = crmFeignClient.statementRegenerate(message);
                    break;
                case STATEMENT_QUESTION_FEEDBACK_PUSH:
                    response = crmFeignClient.statementQuestionFeedback(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.statementReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.statementReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.CREDIT_INPUT)
    public void creditReceive(DistributorMessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case CREDIT_FIXED_LIMIT_CHANGE_PUSH:
                    response = crmFeignClient.creditFixedLimitChange(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.creditReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.creditReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.SIGN_IN_INPUT)
    public void signInReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case SIGN_IN_ADD_PUSH:
                    response = crmFeignClient.signInAdd(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.signInReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.signInReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.WORK_REPORT_INPUT)
    public void workReportReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case WORK_REPORT_PUSH:
                    response = crmFeignClient.workReportPush(message);
                    break;
                case WORK_REPORT_ADD_PUSH:
                    response = crmFeignClient.workReportAdd(message);
                    break;
                case WORK_REPORT_COMMENT_LIKE_PUSH:
                    response = crmFeignClient.workReportLike(message);
                    break;
                case WORK_REPORT_COMMENT_PUSH:
                    response = crmFeignClient.workReportComment(message);
                    break;
                case WORK_REPORT_COMMENT_DEL_PUSH:
                    response = crmFeignClient.workReportCommentDel(message);
                    break;
                case WORK_REPORT_AT_PUSH:
                    response = crmFeignClient.workReportAt(message);
                    break;
                case WORK_REPORT_COMMENT_UNREAD_PUSH:
                    response = crmFeignClient.workReportUnRead(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.workReportReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.workReportReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.QUOTATION_INPUT)
    public void quotationReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case QUOTATION_ADD_PUSH:
                    response = crmFeignClient.quotationAdd(message);
                    break;
                case QUOTATION_STATUS_PUSH:
                    response = crmFeignClient.quotationChange(message);
                    break;
                case QUOTATION_TEAM_ADD_DEL_MAIN_PUSH:
                    response = crmFeignClient.quotationTeamMain(message);
                    break;
                case QUOTATION_TEAM_ADD_DEL_SYNERGETIC_PUSH:
                    response = crmFeignClient.quotationTeamSynergetic(message);
                    break;
                default:
                    break;

            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.quotationReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.quotationReceive Exception, type=" + type, e);
        }
    }
    
    @StreamListener(value = CrmBinderConstant.PAY_PLAN_INPUT)
    public void payPlanReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case PAY_PLAN_ADD:
                    response = crmFeignClient.payPlanAdd(message);
                    break;
                case PAY_SHEET_ADD:
                    response = crmFeignClient.paySheetAdd(message);
                    break;
                case PAY_SHEET_RED_ADD:
                    response = crmFeignClient.paySheetRedAdd(message);
                    break;
                case PAY_SHEET_BAD_ADD:
                    response = crmFeignClient.paySheetBadAdd(message);
                    break;
                case PAY_SHEET_PREPAY_ADD:
                    response = crmFeignClient.paySheetPrepayAdd(message);
                    break;
                case PAY_SHEET_RED_PREPAY_ADD:
                    response = crmFeignClient.paySheetRedPrepayAdd(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.payPlanReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.payPlanReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = CrmBinderConstant.PAYMENT_TASK_INPUT)
    public void paymentTaskReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case PAYMENT_TASK_ADD_PUSH:
                    response = crmFeignClient.paymentTaskAdd(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("CrmListener.paymentTaskReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("CrmListener.paymentTaskReceive Exception, type=" + type, e);
        }
    }

}
